package com.chinasoft.back.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author 中软zhang
 * @create 2023-08-24 16:11
 */
@PropertySource("classpath:db.properties")
public class JdbcConfig {

    @Value("${jdbc.driver}")
    String driver;
    @Value("${jdbc.url}")
    String url;
    @Value("${jdbc.username}")
    String username;
    @Value("${jdbc.password}")
    String password;

    // 得到数据库连接池
    @Bean
    public DataSource getDataSource() {
        DruidDataSource dds = new DruidDataSource();
        dds.setDriverClassName(this.driver);
        dds.setUrl(this.url);
        dds.setUsername(this.username);
        dds.setPassword(this.password);
        return dds;
    }

    // 注入事务的管理平台
    @Bean
    public PlatformTransactionManager getTm(DataSource ds) {
        DataSourceTransactionManager dstm = new DataSourceTransactionManager();
        dstm.setDataSource(ds);
        return dstm;
    }

}
